package com.cardbaobao.cardbabyclient.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.model.UserInfo;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.utils.UmengEventUtils;
import com.cardbaobao.cardbabyclient.widget.TitleBar;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by Administrator on 2016/12/29.
 * 修改密码页面
 */
public class UpdatePasswordActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.confirm_pass)
    EditText mConfirmPass;//确认密码
    @BindView(R.id.new_pass)
    EditText mNewPass;//新密码
    @BindView(R.id.old_pass)
    EditText mOldPass;//老密码
    @BindView(R.id.commit_data)
    TextView mSubmitData;

    @Override
    protected void initView(Bundle savedInstanceState) {
        ImmersionBar.with(this).statusBarDarkFont(true,Constant.STATUS_BAR_FONT_ALPHA).init();
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mCurrentPageId= Self.Page.app_modify_login_pwd_page;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mSubmitData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String oldPass = mOldPass.getText().toString().trim();
                String newPass = mNewPass.getText().toString().trim();
                String confirm = mConfirmPass.getText().toString().trim();
                String msg = "";
                if (oldPass.length() >= 6 && newPass.length() >= 6 && confirm.length() >= 6) {
                    if (oldPass.equals(newPass)) {
                        msg = "新密码跟旧密码不能一样";
                    } else if (newPass.equals(confirm)) {
                        SelfUtils.buriedClickData(Self.Action.app_click_modify_login_pwd,
                                Self.Action.app_click_modify_login_pwd_des,
                                mEnterPageTime,
                                mCurrentPageId,
                                mPrePageId);
                        updatePassWord(oldPass, newPass);
                        GoogleAnalyticsUtil.setScreenAction(Google.CAREGORY_ORD, Google.ACTION_MODIFY);
                    } else {
                        msg = "新密码跟确认密码不一致";
                    }
                } else {
                    msg = "密码长度最少为6位";
                }
                if (!msg.isEmpty()) {
                    ToastUtil.showToast(msg);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_update_password;
    }

    @Override
    protected void onResume() {
        super.onResume();
        UmengEventUtils.pageEvent(mContext,
                ApplicationHelper.getApplicationContext().getString(R.string.event_0x024));
        GoogleAnalyticsUtil.setScreenName(Google.UPDATE_PWD);
    }

    /**
     * 修改密码
     *
     * @param oldPass
     * @param newPass
     */
    private void updatePassWord(String oldPass, String newPass) {
        UserInfo userInfo = mApplication.getUser();
        if (userInfo == null) return;
        showProgressDialog(false);
        Observable<String> observable=
                Api.modifyPassword(String.valueOf(userInfo.getID()),oldPass,newPass);
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.
                        getApplicationContext().
                        getString(R.string.string_request_server_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                JSONObject object= JSON.parseObject(s);
                if(object.getIntValue("code")==1){
                    ToastUtil.showToastLong(object.getString("msg"));
                    finish();
                }else{
                    ToastUtil.showToast(object.getString("msg"));
                }
            }
        });
    }

    @Override
    public void showProgressDialog(boolean isTouchHide) {
        mOldPass.setEnabled(false);
        mNewPass.setEnabled(false);
        mConfirmPass.setEnabled(false);
    }

    @Override
    public void dismissProgressDialog() {
        mOldPass.setEnabled(true);
        mNewPass.setEnabled(true);
        mConfirmPass.setEnabled(true);
    }
}
